package tienich360.com.lib;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;

public class ReadFileAssets {
	public static String readAssetsFile(Context paramContext, String paramString) {
		AssetManager assetManager = paramContext.getAssets();
		InputStream inputStream = null;
		try {
			inputStream = assetManager.open(paramString);
			String text = loadTextFile(inputStream);
			return text.trim();
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	public static String loadTextFile(InputStream inputStream)
			throws IOException {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[4096];
		int len = 0;
		while ((len = inputStream.read(bytes)) > 0)
			byteStream.write(bytes, 0, len);
		return new String(byteStream.toByteArray(), "UTF8");
	}
}
